package com.example.myapplication10;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

public class mesAdapter extends ArrayAdapter<Person> {
    private int resourceID;
    public mesAdapter(@NonNull Context context, int resource, List<Person> objs) {
        super(context, resource,objs);
        resourceID = resource;
    }

    public View getView(int position, View convertView, ViewGroup parent){
        Person person = getItem(position);
        View view;
        ViewHolder viewHolder;
        if(convertView == null){
            view = LayoutInflater.from(getContext()).inflate(resourceID,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.name = (TextView) view.findViewById(R.id.name);
            viewHolder.phone = (TextView) view.findViewById(R.id.phone);
            view.setTag(viewHolder);
        }else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
//        将内容显示到主页面
        viewHolder.name.setText(person.getName()+"");
        viewHolder.phone.setText(person.getPhone()+"");
        return view;
    }
    //    内部类，提高效率，减少请求次数
    class ViewHolder{
        TextView name;
        TextView phone;
    }
}
